﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace AkashaMemory
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void AppInit_DoWork(object sender, DoWorkEventArgs e)
        {
            if (Directory.Exists("./.minecraft/mods"))
            {
                MainBox.Text += "目录正确，正在生成本地mod表\r\n";
                if (ModsUpdate.GenModsList())
                {
                    MainBox.Text += "\r\n本地mod表生成完毕，正在拉取服务器mod信息\r\n";

                    if (ModsUpdate.GetModsList(URIBox.Text + "new.xml"))
                    {
                        MainBox.Text += "\r\n服务器mod更新完毕，正在比较mod\r\n";
                        ModsUpdate.CompareMods();
                        foreach (Mod mod in ModsUpdate.LocalAbsent.Mods)
                        {
                            //MainBox.Text += mod.Name + "\r\n";
                            LocalAbsentCheckedList.Items.Add(mod.Name, true);
                            LocalAbsentCheckedList.Enabled = true;
                        }
                        foreach (Mod mod in ModsUpdate.LocalRedundancy.Mods)
                        {
                            //MainBox.Text += mod.Name + "\r\n";
                            LocalRedundancyCheckedList.Items.Add(mod.Name, true);
                            LocalRedundancyCheckedList.Enabled = true;
                        }
                        MainBox.Text += "\r\n如果你同意以上更改，请点击开始更新，则更改生效！\r\n";
                        Button_StartUp.Enabled = true;
                    }
                    else
                    {
                        MainBox.Text += "\r\n服务器mod信息拉取失败，请检查网络和xml格式！\r\n";
                    }

                }
                else
                {
                    MainBox.Text += "\r\n校验出现问题！\r\n";
                }
            }
            else
            {
                MainBox.Text += "\r\n没有找到目录，请检查应用程序是否放置在游戏根目录（与.minecraft目录同级）\r\n";
            }
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            AppInit.RunWorkerAsync();
        }

        private void Button_GenLocalXml_Click(object sender, EventArgs e)
        {

            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Encoding = Encoding.UTF8;
            xmlWriterSettings.Indent = true;


            XmlWriter xmlWriter = XmlWriter.Create("./new.xml", xmlWriterSettings);

            xmlWriter.WriteStartElement("Mods");
            xmlWriter.WriteElementString("ModsNum", ModsUpdate.ModListLocal.Mods.Length.ToString());
            for (int i = 0; i < ModsUpdate.ModListLocal.Mods.Length; i++)
            {
                xmlWriter.WriteStartElement("Mod" + i.ToString());
                xmlWriter.WriteElementString("MD5", ModsUpdate.ModListLocal.Mods[i].MD5);
                xmlWriter.WriteElementString("Name", ModsUpdate.ModListLocal.Mods[i].Name);
                xmlWriter.WriteElementString("Path", ModsUpdate.ModListLocal.Mods[i].Path);
                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();

            xmlWriter.Close();

        }

        private void MainBox_TextChanged(object sender, EventArgs e)
        {
            MainBox.SelectionStart = MainBox.Text.Length;
            MainBox.ScrollToCaret();
        }

        private void Button_StartUp_Click(object sender, EventArgs e)
        {
            ModUpdate.RunWorkerAsync();
            LocalRedundancyCheckedList.Enabled = false;
            LocalAbsentCheckedList.Enabled = false;
            Button_StartUp.Enabled = false;
            Button_GenLocalXml.Enabled = false;
            Button_StartUp.Text = "正在更新";
        }

        private void ModUpdate_DoWork(object sender, DoWorkEventArgs e)
        {
            MainBox.Text += "\r\n开始删除所选冗余mod\r\n";

            for (int i = 0; i < LocalRedundancyCheckedList.Items.Count; i++)
            {
                if (LocalRedundancyCheckedList.GetItemChecked(i))
                {
                    try
                    {
                        File.Delete(ModsUpdate.LocalRedundancy.Mods[i].Path);
                        MainBox.Text += ModsUpdate.LocalRedundancy.Mods[i].Name + "被删除了\r\n";
                    }
                    catch
                    {
                        MainBox.Text+=ModsUpdate.LocalRedundancy.Mods[i].Name+ "删除失败！请检查目录！\r\n";
                        return;
                    }
                    
                }
            }

            MainBox.Text += "\r\n所选冗余mod都被删除了！\r\n";

            MainBox.Text += "\r\n开始安装所选缺失mod\r\n";

            WebClient webClient = new WebClient();

            for (int i = 0; i < LocalAbsentCheckedList.Items.Count; i++)
            {
                if (LocalAbsentCheckedList.GetItemChecked(i))
                {
                    try
                    {
                        string uri = URIBox.Text + "mods/" + ModsUpdate.LocalAbsent.Mods[i].Name;
                        webClient.DownloadFile(uri, "./.minecraft/mods\\" + ModsUpdate.LocalAbsent.Mods[i].Name);
                        MainBox.Text += ModsUpdate.LocalAbsent.Mods[i].Name + "添加成功\r\n";
                    }
                    catch
                    {
                        MainBox.Text+=ModsUpdate.LocalAbsent.Mods[i].Name+ "安装失败！请检查网络和目录！\r\n";
                        return;
                    }
                }

            }

            MainBox.Text += "\r\n所选缺失mod都成功安装！\r\n";

        }
    }
}
